// functionPage/pages/news/adviceList/adviceList.js
import throttle from '../../../../utils/throttle.js';
import Http from '../../../../utils/http.js';
const http = new Http();
const app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        indexSize: 0, //处理意见表记位
        unHandleCnt: 0, //未处理数量
        allCnt: '',
        picurls: [
            'http://static.map8.com/sp/20200908/0_114506332086.jpg',
            'http://static.map8.com/sp/20200908/0_114506332086.jpg',
            'http://static.map8.com/sp/20200908/0_114506332086.jpg',
            'http://static.map8.com/sp/20200908/0_114506332086.jpg',
            'http://static.map8.com/sp/20200908/0_114506332086.jpg',
        ],
        pageNum: 0,
        loading: true,
        loadingAll: false,
        newsArr: [],
    },
    jumpReason(e) {
        this.setData({
            indexSize: e.currentTarget.dataset.index,
        });
        wx.navigateTo({
            url: `../../../../releasePage/pages/reviewReason/reviewReason?id=${e.currentTarget.dataset.id}&fromWhere=${e.currentTarget.dataset.fromwhere}`,
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getNewsList();
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getUnHandleCnt();
    },

    // 预览图片
    previewImage(e) {
        let index = e.currentTarget.dataset.index;
        let images = JSON.parse(e.currentTarget.dataset.images) || [];
        wx.previewImage({
            current: images[index],
            urls: images,
        });
    },
    //查询待处理数量
    getUnHandleCnt() {
        let _that = this;
        //经验跳转
        http.getRequest(
            `cjMap/getUnHandleCnt?authId=${app.globalData.authId}`
        ).then((res) => {
            if (res.status == 0) {
                wx.setNavigationBarTitle({
                    title: `全部建议${res.allCnt ? '·' + res.allCnt : ''}`,
                });
                _that.setData({
                    unHandleCnt: res.unHandleCnt,
                    allCnt: res.allCnt,
                });
            }
        });
    },
    getNewsList() {
        if (!this.data.loadingAll) {
            this.setData({
                pageNum: this.data.pageNum + 1,
                loading: true,
                loadingAll: false,
            });
            let data = {
                authId: app.globalData.authId,
                pageNo: this.data.pageNum,
                pageSize: 20,
            };
            http.getRequest('cjMap/getSysProposalList', data).then((res) => {
                if (res.status == 0) {
                    res.list.forEach((item) => {
                        item.createtime = app.timeago(item.createtime);
                        item.picurl = JSON.parse(item.picurl);
                        this.data.newsArr.push(item);
                    });
                    this.setData({
                        newsArr: this.data.newsArr,
                        loading: false,
                    });

                    if (res.pager.currentPage >= res.pager.totalPages) {
                        this.setData({
                            loadingAll: true,
                        });
                    }
                } else {
                    this.setData({
                        loadingAll: true,
                        loading: false,
                    });
                }
            });
        }
    },
    refreshNewsList() {
        let _that = this;
        http.getRequest(
            `cjMap/getSysProposalList?authId=${
                app.globalData.authId
            }&pageSize=${_that.data.indexSize + 10}&pageNo=1`
        ).then((res) => {
            if (res.status == 0) {
                let index = _that.data.indexSize;
                for (let i = 0; i < res.list.length; i++) {
                    if (_that.data.newsArr[index].id == res.list[i].id) {
                        _that.data.newsArr[index].verifyname =
                            res.list[i].verifyname;
                        _that.data.newsArr[index].verifyflg =
                            res.list[i].verifyflg;
                        _that.data.newsArr[index].reason = res.list[i].reason;
                        _that.data.newsArr[index].verifytime =
                            res.list[i].verifytime;
                        break;
                    }
                }
                _that.setData({
                    newsArr: _that.data.newsArr,
                });
            }
        });
    },
});
